package com.zhn.evsi.controller.System;

import com.zhn.evsi.common.Result;
import com.zhn.evsi.domain.dto.System.SysUserDTO;
import com.zhn.evsi.domain.dto.System.SysUserInfoEditDTO;
import com.zhn.evsi.exception.CustomException;
import com.zhn.evsi.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * 用户管理
 * */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 注册发送邮件
     * */
    @ApiOperation("注册发送邮件")
    @PostMapping("/register")
    public Result register(@RequestBody SysUserDTO sysUserDTO) {
        if (sysUserDTO == null || sysUserDTO.getUsername() == null || sysUserDTO.getPassword() == null) {
            throw new CustomException("账号密码为空");
        }
        iSysUserService.register(sysUserDTO);
        return Result.success();
    }
    /**
     * 验证码激活
     * */
    @ApiOperation("邮件接收激活账户")
    @PostMapping("/codeActivation")
    public Result codeActivation(@RequestBody SysUserDTO sysUserDTO) {
        if (sysUserDTO.getCode() == null || sysUserDTO.getUsername() == null) {
            throw new CustomException("验证码为空");
        }
        iSysUserService.codeActivation(sysUserDTO);
        return Result.success();
    }

    /**
     * 登录
     * */
    @ApiOperation("登录")
    @PostMapping("/login")
    public Result login(@RequestBody SysUserDTO sysUserDTO) {
        iSysUserService.login(sysUserDTO);
        return Result.success();
    }
    
    /**
     * 上传用户头像
     *
     * @param file - 文件流
     * @param state - 文件类型（）
     * */
    @ApiOperation("上传用户头像")
    @PostMapping("/uploadAvatar")
    public Result uploadAvatar(@RequestBody MultipartFile file, Integer state) {
        String avatar = iSysUserService.uploadAvatar(file, state);
        return Result.success(avatar);
    }

    /**
     * 角色切换
     *
     * @param characterId - 角色id
     * */
    @ApiOperation("角色切换")
    @PostMapping("/handoff")
    public Result handoff(@RequestParam Integer characterId) {
        iSysUserService.handoff(characterId);
        return Result.success();
    }

    /**
     * 退出账号或关闭网站
     *
     * */
    @ApiOperation("退出账号或关闭网站")
    @GetMapping("/logout")
    public Result logout(@RequestParam Integer characterId) {
        iSysUserService.logout(characterId);
        return Result.success();
    }

    /**
     * 修改用户信息
     * */
    @ApiOperation("修改用户信息")
    @PostMapping("/editUserInfo")
    public Result editUserInfo(@RequestBody SysUserInfoEditDTO sysUserInfoEditDTO) {
        iSysUserService.editUserInfo(sysUserInfoEditDTO);
        return Result.success();
    }







}
